package com.djnt.catering;

import java.util.ArrayList;
import java.util.HashMap;

import com.djnt.catering.QuickQueryStack.IQuery;
import com.djnt.catering.QuickQueryStack.Item;
import com.djnt.catering.QuickQueryStack.ItemQuery;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class CityMoreSelectListActivity extends Activity {

	private static final String TAG = "CitySearchListActivity";
	protected static final int SELECT_PROVINCE = 0;
	private ListView city_more_listview;
	private AllCitiesAdapter mAllCitiesAdapter;
	private Button btnSelectProvince;
	private myEditText city_more_search_textview;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.city_more_select_list);
		// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
		// R.layout.custom_title_1);

		/*
		 * catering app = (catering) this.getApplication();
		 * app.waitForInitDatabase();
		 */

		btnSelectProvince = (Button) this.findViewById(R.id.btnSelectProvince);
		btnSelectProvince.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(CityMoreSelectListActivity.this,
						SearchProvinceActivity.class);
				startActivityForResult(i, SELECT_PROVINCE);
			}
		});

		city_more_listview = (ListView) this
				.findViewById(R.id.city_more_listview);
		mAllCitiesAdapter = new AllCitiesAdapter(this);
		city_more_listview.setAdapter(mAllCitiesAdapter);
		city_more_listview.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				city_more_search_textview.clearFocus();
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(
						city_more_search_textview.getWindowToken(), 0);
				city_more_listview.requestFocus();
				return false;
			}
		});
		city_more_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int postion, long id) {
				// TODO Auto-generated method stub
				TextView t1 = (TextView) view.findViewById(android.R.id.text1);
				setResultForCityName(t1.getText().toString());
			}
		});

		mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				if (msg.what == catering.MSG_TEXT_CHANGED) {
					String t = (String) msg.obj;
					Log.d(TAG, "onTextChanged - " + t);
					mAllCitiesAdapter.onTextChanged(t);
				}
				super.handleMessage(msg);
			}

		};

		city_more_search_textview = (myEditText) this
				.findViewById(R.id.city_more_search_textview);
		city_more_search_textview.setReveiveHandler(mHandler);

		city_more_listview.requestFocus();
	}

	protected void setResultForCityName(String t) {
		// TODO Auto-generated method stub
		Intent i = new Intent();
		i.putExtra(Settings.Persist.CITY_NAME, t);
		setResult(RESULT_OK, i);
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (requestCode) {
		case SELECT_PROVINCE:
			if (data != null) {
				String t = data.getStringExtra(Settings.Persist.PROVINCE_NAME);
				Log.d(TAG, "SELECT_PROVINCE - " + t + "\r\n" + data);
				if (!t.equals(btnSelectProvince.getText())) {
					btnSelectProvince.setText(t);
					city_more_search_textview.setText("");
					mAllCitiesAdapter.queryWithProvince(t);
				}
			}
			return;
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

}
